import type { TableProps } from 'ant-design-vue'
import { ref } from 'vue'

export function useRowSelection(options?: TableProps['rowSelection']) {
  const rowSelection = ref<TableProps['rowSelection']>({
    fixed: true,
    onChange(selectedRowKeys) {
      if (rowSelection.value) {
        rowSelection.value.selectedRowKeys = selectedRowKeys
      }
    },
    selectedRowKeys: [],
    type: 'checkbox',
    ...options,
  })
  function clearSelectedRowKeys() {
    if (rowSelection.value && rowSelection.value.selectedRowKeys) {
      rowSelection.value.selectedRowKeys.length = 0
    }
  }
  return {
    rowSelection,
    clearSelectedRowKeys,
  }
}
